package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.google.common.collect.Maps;
import com.kingyun.gpsinspection.purificationservice.common.constant.BackMessage;
import com.kingyun.gpsinspection.purificationservice.common.enums.LeaveEnum;
import com.kingyun.gpsinspection.purificationservice.common.utils.CommonUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.LeaveMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.MakeHolidayMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.MakeHoliday;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.MakeHolidayService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 补休假接口实现类
 * @author zjh
 * @since 20170704
 */
public class MakeHolidayServiceImpl implements MakeHolidayService {

    @Autowired
    private MakeHolidayMapper makeHolidayMapper;


    @Autowired
    private LeaveMapper leaveMapper;
    /**
     * 新增补休规则
     *
     * @param makeHoliday
     * @param employeeVo
     * @return
     */
    @Override
    public Map<String, Object> saveMakeHoliday(MakeHoliday makeHoliday, EmployeeVo employeeVo) {
        Map<String,Object> result= Maps.newHashMap();
        String makeTime = makeHoliday.getMakeTime();
        String month=makeTime.substring(0,makeTime.lastIndexOf("-"));
        int exists = makeHolidayMapper.queryExists(month);
        if(exists>0){
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            result.put("msg","次月已经存在相关记录");
        }else{
            makeHoliday.setId(UniqueUtil.uuid());
            makeHoliday.setRemainDays(makeHoliday.getDays());
            makeHoliday.setCreateUserId(employeeVo.getEmployeeId());
            makeHoliday.setModifyUserId(employeeVo.getEmployeeId());
            makeHoliday.setCreateTime(CommonUtil.getTimeString());
            makeHoliday.setModifyTime(CommonUtil.getTimeString());
            int insertCount = makeHolidayMapper.insertSelective(makeHoliday);
            if(insertCount>0){
                result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                result.put("msg", BackMessage.ADD_OK);
            }else{
                result.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                result.put("msg", BackMessage.ADD_FAIL);
            }
        }

        return result;
    }

    /**
     * 修改补休规则
     *
     * @param makeHoliday
     * @param employeeVo
     * @return
     */
    @Override
    public Map<String, Object> updateMakeHoliday(MakeHoliday makeHoliday, EmployeeVo employeeVo) {
        Map<String,Object> result= Maps.newHashMap();
        makeHoliday.setRemainDays(makeHoliday.getDays());
        makeHoliday.setModifyUserId(employeeVo.getEmployeeId());
        makeHoliday.setModifyTime(CommonUtil.getTimeString());
        makeHoliday.setMakeTime(null);
        int modifyCount = makeHolidayMapper.updateByPrimaryKeySelective(makeHoliday);
        if(modifyCount>0){
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            result.put("msg", BackMessage.MODIFY_OK);
        }else{
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            result.put("msg", BackMessage.MODIFY_FAIL);
        }
        return result;
    }

    /**
     * 查询所有的补休规则
     *
     * @param makeHoliday
     * @return
     */
    @Override
    public Map<String, Object> listMakeHolidayByPage(MakeHoliday makeHoliday) {
        Map<String,Object> result=Maps.newHashMap();
        List<MakeHoliday> makeHolidays = makeHolidayMapper.queryMakeHolidayByPage(makeHoliday);
        Integer count = makeHolidayMapper.queryMakeHolidayCount(makeHoliday);
        result.put("rows",makeHolidays);
        result.put("total",count);
        return result;
    }

    /**
     * 删除补休规则
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> deleteById(String id) {
        Map<String,Object> result=Maps.newHashMap();
        int deleteCount = makeHolidayMapper.deleteByPrimaryKey(id);
        if(deleteCount>0){
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            result.put("msg", "删除成功");
        }else{
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            result.put("msg", "删除失败");
        }
        return result;
    }

    /**
     * 得到剩余的天数
     *
     * @param month
     * @param employeeId
     * @return
     */
    @Override
    public Integer getRemianDays(String month, String employeeId) {
        int runningTypeDays = leaveMapper.getRunningTypeDays(employeeId, LeaveEnum.MARKERD.getCode());
        int remainDays = makeHolidayMapper.queryDaysOfMonth(month, employeeId);
        return (remainDays-runningTypeDays);
    }

    /**
     * 得到补休规则
     *
     * @param id
     * @return
     */
    @Override
    public MakeHoliday getById(String id) {
        return makeHolidayMapper.selectByPrimaryKey(id);
    }
}
